package com.forum.controller.talk;

import com.common.entity.ApiResult;
import com.common.entity.PageEntity;
import com.common.vo.LatestPosts;
import com.common.vo.PostsDetailVo;
import com.common.vo.RecommendTalkInfoVo;
import com.forum.param.PostsQueryParam;
import com.forum.param.TalkPostsCommentQueryParam;
import com.forum.param.TalkPostsCommentReplyQueryParam;
import com.forum.param.TalkQueryParam;
import com.forum.pojo.talk.TalkList;
import com.forum.pojo.talk.TalkPosts;
import com.forum.pojo.talk.TalkPostsComment;
import com.forum.pojo.talk.TalkPostsCommentReply;
import com.forum.service.talk.TalkListService;
import com.forum.service.talk.TalkPostsCommentReplyService;
import com.forum.service.talk.TalkPostsCommentService;
import com.forum.service.talk.TalkPostsService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author Tsing
 */
@RestController
@RequestMapping("talks")
//@CrossOrigin(origins = "*")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class TalkController {
    private final TalkListService talkListService;
    private final TalkPostsService postsService;
    private final TalkPostsCommentService commentService;
    private final TalkPostsCommentReplyService replyService;

    /**
     * 创建论坛
     * **/
    @PostMapping("/create")
    @Transactional(rollbackFor = Exception.class)
    public ApiResult<String> createTalk(@RequestBody TalkList talkList){
       return talkListService.createTalk(talkList);
    }

    /**
     * 论坛查询
     * **/
    @GetMapping("/list")
    public ApiResult<Map<String,Object>> getTalks(TalkQueryParam param){
        return talkListService.getTalks(param);
    }

    /**
     * 获取当前用户管理的论坛
     * **/
    @GetMapping("/currentUser")
    public ApiResult<Map<String,Object>> getTalksByCurrentUser(PageEntity page){
        return talkListService.getTalksByCurrentUser(page);

    }

    /**
     * 创建帖子
     * **/
    @PostMapping("/posts/create")
    @Transactional(rollbackFor = Exception.class)
    public ApiResult<String> createPosts(@RequestBody TalkPosts posts){
       return postsService.createPosts(posts);
    }

    /**
     * 获取指定论坛的帖子
     * **/
    @GetMapping("/posts/list")
    public ApiResult<Map<String,Object>> getPostsByTalk(PostsQueryParam queryParam){
        return postsService.getPostsByTalk(queryParam);
    }

    /**
     * 获取帖子详情
     * **/
    @GetMapping("/posts/detail/{postId}")
    public ApiResult<PostsDetailVo> getPostDetail(@PathVariable Integer postId){
       return postsService.getPostDetail(postId);
    }

    /**
     * 指定帖子的评论获取
     * **/
    @GetMapping("/posts/comment/list")
    public ApiResult<Map<String,Object>> getCommentList(TalkPostsCommentQueryParam queryParam){
        return commentService.getCommentList(queryParam);
    }

    /**
     * 帖子的评论发送
     * **/
    @PostMapping("/posts/comment/send")
    @Transactional(rollbackFor = Exception.class)
    public ApiResult<String> sendComment(@RequestBody TalkPostsComment comment){
        return commentService.sendComment(comment);
    }

    /**
     * 帖子的评论回复
     * **/
    @PostMapping("/posts/comment/reply")
    @Transactional(rollbackFor = Exception.class)
    public ApiResult<String> replyComment(@RequestBody TalkPostsCommentReply reply){
        return replyService.reply4Comment(reply);
    }

    /**
     * 分页获取指定评论的回复
     * **/
    @GetMapping("/posts/comment/reply/list")
    public ApiResult<Map<String,Object>> getReplyList(TalkPostsCommentReplyQueryParam queryParam){
       return  replyService.getReplyList(queryParam);
    }

    /**
     * 获取最近发布的帖子
     * **/
    @GetMapping("/latest")
    public ApiResult<Map<String,Object>> getLatestTalks(PageEntity pageEntity){
        return talkListService.getLatestTalks(pageEntity);
    }

    @GetMapping("/recommend")
    public ApiResult<List<RecommendTalkInfoVo>> getRecommendPost(){
        return  postsService.getRecommendPost();
    }

    @GetMapping("/talk/{talkId}")
    public ApiResult<?> getTalkById(@PathVariable Long talkId){
        return talkListService.getTalkById(talkId);
    }

    @GetMapping("/hotPost/{talkId}")
    public ApiResult<?> getHotPostByTalk(@PathVariable Long talkId){
        return talkListService.getHotPostsBtTalk(talkId);
    }
}
